package com.datastructure.test.getupmedian;

public class GetUpMedian {

    public static void main(String[] args) {
        GetUpMedian getUpMedian = new GetUpMedian();
        int[] arr1 = {1,2,3};
        int[] arr2 = {3,4,5};
        System.out.println(getUpMedian.getUpMedian(arr1,arr2));
    }

    public int getUpMedian (int[] arr1, int[] arr2) {
        // write code here
        int arr1Length = arr1.length;
        int arr2Length = arr2.length;
        int media = (arr1Length+arr2Length+1)/2;
        int i = 0;
        int j = 0;
        int num = 0;
        int result = 0;
        while (num<media){
            if(i<arr1Length&&j<arr2Length){
                if(arr1[i]<=arr2[j]){
                    num++;
                    result=arr1[i];
                    i++;
                } else {
                    num++;
                    result=arr2[j];
                    j++;
                }
            } else {
                num++;
                if(i>=arr1Length){
                    result=arr2[j];
                    j++;
                } else {
                    result=arr1[i];
                    i++;
                }
            }
        }
        return result;
    }
}
